<?php
require_once BaseUtil::getModulePath("base") . '/models/BaseDao.php';

class ProductImageDao extends BaseDao{

   protected $_name;

    public function  _setup() {
        $this->_name = $this->prefix() . "product_images";
        parent::_setup();
    }

    public function getItemImagesByItemId($itemId){
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where('is_delete = 0');
        $select->where('item_id = ?', $itemId);
        $select->order('sort_order');
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    
    public function getImageById($id){
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where('is_delete = 0');
        $select->where('id = ?', $id);
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    
    public function deleteImage($id){
        $where = $this->db_w->quoteInto('id = ?', $id);
        $this->delete($where);
    }
    
    //将原先的默认图片设置为非默认图片
    public function updateDefaultImageStatusByItemId($itemId){
        $imageStatus = array("default_item_image" => 0);
        $where = $this->db_w->quoteInto('item_id = ?', $itemId);
        $this->updateData($imageStatus, $where);
    }
    
    //将原先的默认图片设置为默认图片
    public function updateNotDefaultImageStatusById($id, $sort){
        $imageStatus = array("default_item_image" => 0, "sort_order" => $sort);
        $where = $this->db_w->quoteInto('id = ?', $id);
        $this->updateData($imageStatus, $where);
    }
    
    /**
     * $id is the id of table
     */
//    public function updateDefaultImageById($id, $sort, $defaultItemImage){
    public function updateImageById($id, $sort, $defaultItemImage){
        $imageStatus = array("default_item_image" => 1, "sort_order" => $sort, "default_item_image" => $defaultItemImage);
        $where = $this->db_w->quoteInto('id = ?', $id);
        $this->updateData($imageStatus, $where);
    }
    
    //设置图片顺序
    public function updateImageSortById($id, $sortOrder){
        $imageStatus = array("sort_order" => $sortOrder);
        $where = $this->db_w->quoteInto('id = ?', $id);
        $this->updateData($imageStatus, $where);
    }
    
    //判断当前产品是否设置了默认图片
    public function getDefaultImageByItemId($itemId){
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where('item_id = ?', $itemId);
        $select->where('default_item_image = 1');
        $select->where('is_delete = 0');
        $select->order('sort_order');
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    
}